package ar.edu.uba.fi.jmit.monitorengine.parser;

import java.io.IOException;

import ar.edu.uba.fi.jmit.monitorengine.MonitorEngineTestCase;
import ar.edu.uba.fi.jmit.monitorengine.backend.client.JMitFileService;
import ar.edu.uba.fi.jmit.monitorengine.backend.server.JMitFileServiceImpl;
import ar.edu.uba.fi.jmit.monitorengine.parser.exceptions.XMLParserRuntimeException;

public class ParsersManagerTestCase extends MonitorEngineTestCase {
	
	private static String RESOURCES_PATH = "src/test/resources";
	private static String fileWithoutProblems = "<jmit>\n" + 
	"<monitorengine>\n" +
	"	<connection protocol=\"http\" ip=\"10.0.0.1\" port=\"8080\" context=\"monitorEngine\"/>\n" +
	"</monitorengine>\n" +
	"<monitor name=\"testInvocationCounter4\" enviroment=\"development\" application=\"Prueba\" type=\"InvocationCounter\">\n" +
	"	<configurations>\n" + 
	"		<configuration name=\"methodConfiguration\">\n" + 
	"			<method name=\"getX\" return=\"java.lang.Integer\" class=\"ar.edu.uba.fi.jmit.aspecter.itegration.testUtils.PointTestInvocationCounter\" id=\"x\">\n" +
	"				<param name=\"data\" type=\"int\"/>\n" + 
	"			</method>\n" + 
	"		</configuration>\n" +
	"	</configurations>\n" +
	"</monitor>\n" +
	"</jmit>\n";
	
	private static String fileWithProblems = "<jmit>\n" + 
	"<monitorengine>\n" +
	"	<connection protocol=\"http\" ip=\"10.0.0.1\" port=\"8080\" context=\"monitorEngine\"/>\n" +
	"</monitorengine>\n" +
	"<monitor name=\"testInvocationCounter4\" enviroment=\"development\" application=\"Prueba\">\n" +
	"	<configurations>\n" + 
	"		<configuration name=\"methodConfiguration\">\n" + 
	"			<method name=\"getX\" return=\"java.lang.Integer\" class=\"ar.edu.uba.fi.jmit.aspecter.itegration.testUtils.PointTestInvocationCounter\" id=\"x\">\n" +
	"				<param name=\"data\" type=\"int\"/>\n" + 
	"			</method>\n" + 
	"		</configuration>\n" +
	"	</configurations>\n" +
	"</monitor>\n" +
	"</jmit>\n";

	public void testEmptyFolder() {
		try {
			ParsersManager.loadConfigurations(RESOURCES_PATH + "/EmptyFolder");
			fail();
		} catch (XMLParserRuntimeException e) {
			assertEquals(e.getMessage(), "The directory \"" + RESOURCES_PATH + "/EmptyFolder" + "\" is empty.");
		}
	}
	
	public void testFolderWithProblems() {
		try {
			ParsersManager.loadConfigurations(RESOURCES_PATH + "/FolderWithProblems");
			fail();
		} catch (XMLParserRuntimeException e) {
			assertEquals(e.getMessage(), "There are problems with these files: ToFail.jmit.xml.");
		}
	}
	
	public void testLoadFileWithProblems() {
		String filename = "ParsersManagerTestCase.jmit.xml";
		try {
			ParsersManager.loadFile(fileWithProblems, filename);
			fail();
		} catch (XMLParserRuntimeException e) {
			assertEquals(e.getMessage(), "The file is not valid");
		} catch (IOException e2) {
			fail(e2);
		}
	}
	
	public void testLoadFileWithoutProblems() {
		String filename = "ParsersManagerTestCase.jmit.xml";
		try {
			ParsersManager.loadFile(fileWithoutProblems, filename);
		} catch (XMLParserRuntimeException e) {
			fail(e);
		} catch (IOException e2) {
			fail(e2);
		}
		
		JMitFileService service = new JMitFileServiceImpl();
		service.deleteFile(filename);
	}
	
}
